import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/cupertino.dart';
import 'package:ls_work/net/post_request.dart';
import 'package:ls_work/net/response/use_request/goods_request_list_data.dart';
import 'package:ls_work/net/response/use_request/goods_request_type_list.dart';
import 'package:ls_work/net/response/use_request/new_goods_list_data.dart';
import 'package:ls_work/net/response/use_request/use_request_list_data.dart';
import 'package:ls_work/net/response/use_request/use_request_record_detail_data.dart';

import '../../net/api/use_request_apis.dart';

class UseRequestModel extends BaseMvvmModel {
  @override
  void onCleared() {}

  ///申请记录
  Future<List<UseRequestListData>> requestRecord(
      {String status = "",
      String officeId = "",
      String createDateStart = "",
      String createDateEnd = "",
      required int rows,
      required int page}) async {
    var map = {
      "status": status,
      "officeId": officeId,
      "createDateStart": createDateStart,
      "createDateEnd": createDateEnd,
      "rows": rows,
      "page": page,
    };
    var data = await HttpGo.instance.postData(UseRequestApis.requestRecord,
        data: map, options: PostRequest.urlencodedHeader);
    if ("0" == data['code']) {
      var list = ((data["data"]["rows"] as List) ?? [])
          .map((e) => UseRequestListData.fromJson(e))
          .toList();
      return list;
    } else {
      throw data['msg'];
    }
  }

  ///物资类型
  Future<List<GoodsRequestTypeList>> goodsTypeList() async {
    var data = await HttpGo.instance.postData(UseRequestApis.goodTypeLit,
        options: PostRequest.urlencodedHeader);
    if ("0" == data['code']) {
      var list = ((data["data"]["rows"] as List) ?? [])
          .map((e) => GoodsRequestTypeList.fromJson(e))
          .toList();
      return list;
    } else {
      throw data['msg'];
    }
  }

  ///物资列表
  Future<List<GoodsRequestListData>> goodsList(
      {String materialName = "",
      String materialType = "",
      String assetType = "",
      String useRange = "0",
      int rows = 100}) async {
    var map = {
      "materialName": materialName,
      "materialType": materialType,
      "assetType": assetType,
      "rows": rows,
      "useRange": useRange,
    };
    var data = await HttpGo.instance.postData(UseRequestApis.goodsList,
        data: map, options: PostRequest.urlencodedHeader);
    if ("0" == data['code']) {
      var list = ((data["data"]["rows"] as List) ?? [])
          .map((e) => GoodsRequestListData.fromJson(e))
          .toList();
      return list;
    } else {
      throw data['msg'];
    }
  }

  Future<List<NewGoodsListData>> newGoodsList(
      {String maintenanceNameLike = "",
      dynamic fixedAssets,
      dynamic donate,
        String materialNameNoLike = "",
      int page = 1,
      int rows = 20}) async {
    var map = {
      "maintenanceNameLike":maintenanceNameLike,
      "materialNameNoLike":materialNameNoLike,
      "fixedAssets":fixedAssets,
      "donate":donate,
      "page":page,
      "rows":rows,
    };
    map.removeWhere((key, value) => value==null);
    var data = await HttpGo.instance.postData(UseRequestApis.newGoodsList,
        data: map, options: Options(contentType: Headers.formUrlEncodedContentType));
    if("0"==data['code']){
      var list = ((data["data"]["rows"] as List) ?? [])
          .map((e) => NewGoodsListData.fromJson(e))
          .toList();
      return list;
    }else{
      throw data['msg'];
    }
  }

  ///提交申请
  Future<Map> addRequest({
    dynamic id,
    String person = "",
    String oddNumbers = '',
    String mechanism = '',
    String remark = '',
    String useTo = "",
    required dynamic officeId,
    bool canAudite = true,
    required dynamic officeChildrenId,
    required dynamic applyPersonId,
    int bpmType = 0,
    String useRange = "0",
    String useRangeName = "",
    String bpmTypeName = "常规审批",
    required List<Map> logisticsMaterialApplicationDetailBeans,
    required dynamic saveOrSubmit,
  }) async {
    var map = {
      "person": person,
      "oddNumbers": oddNumbers,
      "mechanism": mechanism,
      "remark": remark,
      "officeId": officeId,
      "canAudite": canAudite,
      "officeChildrenId": officeChildrenId,
      "applyPersonId": applyPersonId,
      "bpmType": bpmType,
      "useTo": useTo,
      "bpmTypeName": bpmTypeName,
      "logisticsMaterialApplicationDetailBeans":
          logisticsMaterialApplicationDetailBeans,
      "saveOrSubmit": saveOrSubmit,
      "useRange": useRange,
      "useRangeName": useRangeName,
    };

    var url = "";
    if (id != null) {
      map['id'] = id;
      url = UseRequestApis.updateRequest;
    } else {
      url = UseRequestApis.addRequest;
    }

    var data = await HttpGo.instance.postData(url,
        data: map.toJsonMapStr(), options: PostRequest.urlencodedHeader);
    if ("0" == data['code']) {
      return map;
    } else {
      throw data['msg'];
    }
  }

  ///获取申请详情
  Future<UseRequestRecordDetailData> requestDetail(String id) async {
    var map = {"id": id};
    var data = await HttpGo.instance.postData(UseRequestApis.requestDetail,
        data: map, options: PostRequest.urlencodedHeader);
    if ("0" == data['code']) {
      return UseRequestRecordDetailData.fromJson(data['data']);
    } else {
      throw data['msg'];
    }
  }

  ///删除申请
  Future<Map> deleteRequest(dynamic id) async {
    var map = {"id": id};
    var data = await HttpGo.instance.postData(UseRequestApis.deleteRequest,
        data: map, options: PostRequest.urlencodedHeader);
    if ("0" == data['code']) {
      return data;
    } else {
      throw data['msg'];
    }
  }
}
